<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021-04-29
 * Time: 16:18
 */
namespace app\admin\controller\system;
use app\admin\controller\command\AdminController;
use app\admin\controller\command\ControllerAnnotation;
use app\admin\controller\command\NodeAnotation;
use app\admin\model\system\SystemField;
use app\admin\model\traits\Curd;
use app\admin\model\traits\TablHandle;
use think\App;
use think\facade\Db;
use think\Request;
/**
 * @ControllerAnnotation(title="字段管理")
 * Class Auth
 * @package app\admin\controller\system
 */
class Field extends AdminController
{
    use Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new SystemField();
        $array = TablHandle::getClassName(__CLASS__);
        $str = $array[(count($array)-1>=0?count($array)-1:0)];
        if(is_file(__DIR__.DS.'field'.DS.$str.'.txt')){
            $arr = file_get_contents(__DIR__.DS.'field'.DS.$str.'.txt');
            $this->AllData = json_decode($arr,true);
//            echo '<pre>';
//            print_r($this->AllData);
//            exit;
        }else{
            $this->AllData = [
                'field'=>[]
            ];
        }
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function index(Request $request)
    {
        if($request->isAjax()){
            $limit = $request->param('limit',10);
            $where = $this->buildTableParames();
            $table_id = $request->param('table_id',null);
            if($table_id){
                $models = Db::name('system_model')
                    ->where('id',$table_id)
                    ->find();
                if(empty($models)){
                    return json_page_response([]);
                }
                $where[] = ['table','=',$models['table']];
            }
//        echo '<pre>';
//        print_r($where);
//        exit;
            if(empty($where)){
                $data = $this->model->order('sort desc,id asc')->paginate($limit)->toArray();
            }else{
                $data = $this->model->where($where)->order('sort desc,id asc')->paginate($limit)->toArray();
            }
            $data['data'] = $this->arrangementData($data['data']);
//        echo '<pre>';
//        print_r($data);
//        exit;
            return json_page_response($data);
        }
        $data = $request->param();
        if(isset($data['id'])&&isset($data['field'])&&$data['id']&&$data['field']){
            $parameter = "?".$data['field'].'='.$data['id'];
        }else{
            $parameter = '';
        }
        $this->assign('parameter',$parameter);
//        echo $parameter;
//        exit;
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="添加")
     */
    public function add(Request $request)
    {
        $field = $this->AllData['field'];
        if($request->isAjax()){
//            $data = [
//                'describe'=>'',
//                'edit'=>1,
//                'export'=>0,
//                'field'=>'ces',
//                'foreign_key'=>'',
//                'formtype'=>'',
//                'is_null'=>0,
//                'join_table'=>'',
//                'lang'=>'255',
//                'name'=>'测试',
//                'option'=>'',
//                'search'=>0,
//                'show'=>1,
//                'sort'=>'100',
//                'status'=>'on',
//                'table'=>'on',
//            ];
            $data = $this->SubmitDataArray();
            if(!(isset($data['create_time'])&&$data['create_time'])){
                $data['create_time'] = time();
            }
            if(!(isset($data['update_time'])&&$data['update_time'])){
                $data['update_time'] = time();
            }
            $bool = TablHandle::AddField($data);
//            return $this->error('失败',202,$bool);
            if(!$bool){
                return $this->error('添加失败');
            }
//            return $this->error('添加失败',202,$data);
            $bool = $this->model->save($data);
//            return $this->error('添加失败',202,$data);
            if($bool){
                return $this->success('添加成功');
            }else{
                return $this->error('添加失败');
            }
        }
        $data = $this->arrangementData();
        $id = $request->param('table_id',null);
        if(empty($id)){
            return $this->error('请输入表名');
        }
//        $model = Db::name('system_model')
//            ->where('id',7)
//            ->findOrEmpty();
//        $field = Db::name('system_field')
//            ->where('table','system_field')
//            ->select()->toArray();
//        $newfield = [];
//        foreach ($field as $value){
//            if(isset($value['option'])&&$value['option']){
//                $value['option'] = TablHandle::optionToArray($value['option']);
//            }
//            $newfield[$value['field']] = $value;
//        }
//        $model['field'] = $newfield;
//        file_put_contents(root_path().'app'.DS.'admin'.DS.'controller'.DS.'system'.DS.'field'.DS.'Field.txt',json_encode($model,JSON_UNESCAPED_UNICODE));
//        echo '<pre>';
//        print_r($model);
//        exit;




        $model = Db::name('system_model')
            ->where('id',$id)
            ->findOrEmpty();
        $data['table'] = $model['table'];

        $this->assign('data',$data);
        $field = TablHandle::conversionField($field);
//        echo '<pre>';
//        print_r($field);
//        exit;
        $this->assign('field',$field);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit(Request $request)
    {
        $field = $this->AllData['field'];
        if($request->isAjax()){
            $data = $this->SubmitDataArray();
            if(empty($data['id'])){
                return $this->error('请输入要修改的id');
            }
            if(!(isset($data['update_time'])&&$data['update_time'])){
                $data['update_time'] = time();
            }
//            return $this->error('添加失败',202,$data);
            $mo = $this->model->find($data['id']);
            $bool = TablHandle::UpdateField($mo->toArray(),$data);
//            return $this->error('修改失败',202,$bool);
            if(!$bool){
                return $this->error('修改失败');
            }
            $bool = $mo->save($data);
            if($bool){
                return $this->success('修改成功');
            }else{
                return $this->error('修改失败');
            }
        }
        $tiaojian = $request->param();
        $data = $this->model->find($tiaojian['id'])->toArray();
        foreach ($tiaojian as $key=>$value){
            $data[$key] = $value;
        }
        $this->assign('data',$data);
        $field = TablHandle::conversionField($field);
        $this->assign('field',$field);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="删除")
     */
    public function delete(Request $request)
    {
        $data = $request->param();
//        return $this->error('删除失败',202,$data);
        $bool = TablHandle::DeleteField($data['id']);
//        return $this->error('删除失败',202,$bool);
        if(!$bool){
            return $this->error('删除失败');
        }
        $bool = $this->model->destroy($data['id']);
        if($bool){
            return $this->success('删除成功',$data);
        }else{
            return $this->error('删除失败');
        }
    }
}